package com.xinkao.example.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinkao.example.domain.entity.ExampleEntity;
import com.xinkao.example.dao.ExampleDao;
import com.xinkao.example.domain.entity.UploadFileEntity;
import com.xinkao.example.domain.excel.ExampleInputErrEntity;
import com.xinkao.example.service.ExampleService;
import com.xinkao.example.util.CommonUtil;
import javax.annotation.Resource;

import com.xinkao.example.util.RandomUtils;
import com.xinkao.example.util.RedisUtils;
import com.xinkao.example.util.constants.ErrorEnum;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 示例表
 * @Author lililiang
 * @Date 2021-08-20 17:38:54
 */
@Service("exampleService")
public class ExampleServiceImpl extends ServiceImpl<ExampleDao, ExampleEntity>
        implements ExampleService {

    @Resource
    private ExampleDao dao;
    @Resource
    private RedisUtils redisUtils;


    /**
     * 分页查询
     */
    @Override
    public JSONObject list4Page(JSONObject jsonObject){
        Page<ExampleEntity> page = CommonUtil.getPage(jsonObject);
        dao.list4Page(page, jsonObject);
        return CommonUtil.successPage(page);
    }

    /**
     * 条件查询
     */
    @Override
    public JSONObject listByCondition(JSONObject jsonObject) {
        return CommonUtil.successJson(dao.listByCondition(jsonObject));
    }
    /**
     * 逻辑删除多条记录
     */
    @Override
    @Transactional
    public JSONObject deleteLogic(List<String> list, String userId) {
        this.update(new LambdaUpdateWrapper<>(ExampleEntity.class)
                .set(ExampleEntity::getDeleteStatus, 1)
                .set(ExampleEntity::getUpdateBy, userId)
                .in(ExampleEntity::getId, list));
        return CommonUtil.successJson();
    }

    /**
     * 根据id查询详情
     */
    @Override
    public JSONObject queryOneById(String id) {
        return CommonUtil.successJson(dao.queryOneById(id));
    }


    /**
     * 保存或修改
     */
    @Override
    @Transactional
    public JSONObject insertOrEdit(ExampleEntity entity) {
        // 处理文件
        List<UploadFileEntity> fileList = entity.getFileList();
        if(CollUtil.isNotEmpty(fileList)){
            StringBuffer docFileId = new StringBuffer();
            for(UploadFileEntity file : fileList){
                docFileId.append(file.getFileId()).append(",");
            }
            entity.setDocFileid(docFileId.substring(0, docFileId.length() - 1));
        }
        return CommonUtil.successJson(this.saveOrUpdate(entity));
    }

    /**
     * 导入excel
     * @Author lililiang
     * @Date 2021/8/20 17:54
     */
    @Override
    public JSONObject inputExcel(List<ExampleInputErrEntity> list) {
        List<ExampleInputErrEntity> errList = new ArrayList<>();
        List<ExampleInputErrEntity> rightList = new ArrayList<>();
        // 校验数据
        for(ExampleInputErrEntity item : list){
            if(StrUtil.isEmpty(item.getExampleTitle())){
                item.setErrMsg("标题不能为空");
                errList.add(item);
                continue;
            }
            rightList.add(item);
        }
        // 批量保存
        if(CollUtil.isNotEmpty(rightList)){
            this.saveBatch(JSON.parseArray(JSON.toJSONString(rightList), ExampleEntity.class));
        }

        // 判断是否存在导入错误
        if(errList.size() > 0){
            String param = RandomUtils.getUUID();
            redisUtils.set(param, errList);
            JSONObject data = new JSONObject();
            data.put("paramData",param);
            return CommonUtil.errorJsonData(ErrorEnum.E_20013,data);
        }else{
            return CommonUtil.successJson();
        }
    }
}
